//
//  SQAccidentTypesViewController.m
//  steel
//
//  Created by 苏青 on 2021/8/5.
//

#import "SQAccidentTypesViewController.h"

#import "SQAccidentTypesModel.h"

#import "CrazyFunction.h"

@interface SQAccidentTypesViewController ()<BaseTableVCInterface>
@property (strong, nonatomic) NSMutableArray *dataArrayM;
@property (strong, nonatomic) NSMutableArray *dataArrayMID;
@end

@implementation SQAccidentTypesViewController

-(NSMutableArray *)dataArrayM{
    if (!_dataArrayM) {
        _dataArrayM = NSMutableArray.new;
    }
    return _dataArrayM;
}

-(NSMutableArray *)dataArrayMID{
    if (!_dataArrayMID) {
        _dataArrayMID = NSMutableArray.new;
    }
    return _dataArrayMID;
}


- (void)viewDidLoad {
    self.tableViewInterface = self;
    [super viewDidLoad];
self.navigationItem.title = @"应急预案类型";
    [self getAccidentTypesData];
    SQ_LOG(@"%@",self.accidentTypeIDs);
    
    LC_WEAK_SELF
    self.cellEvent = ^(int key) {
        SQAccidentTypesModelData * dataModel = weakSelf.dataSouece[key];
        if ([weakSelf.dataArrayMID containsObject:dataModel.ep_id]){
            [weakSelf.dataArrayMID removeObject:dataModel.ep_id];
            dataModel.picStr = @"0";
            [weakSelf.dataArrayM removeObject:dataModel];
        }else{
            [weakSelf.dataArrayMID addObject:dataModel.ep_id];
            dataModel.picStr = @"1";
            [weakSelf.dataArrayM addObject:dataModel];
        }
        [weakSelf reloadTableView];
    };
    
}

-(void)getAccidentTypesData{
    [SQAccidentTypesModel getDataWithParams:@{@"ep_enterprise":MEnterpriseId} WithCallBack:^(SQAccidentTypesModel * data) {
    
        for (SQAccidentTypesModelData * danger in data.data) {
            for (SQAccidentTypesModelData * dangerModel  in self.accidentTypeIDs) {
                if ([danger.ep_id isEqualToString:dangerModel.ep_id]) {
                    [self.dataArrayMID addObject:danger.ep_id];

                    [self.dataArrayM addObject:danger];
                    danger.picStr = @"1";
                    break;
                }
            }
            if (![danger.picStr isEqualToString:@"1"]) {
                danger.picStr = @"0";
            }
        }
        [self detailModelwithArr:data.data withCallBack:nil];
        [self reloadTableView];
        
    }];
}


-(NSInteger)rowsCount:(NSInteger)section{
    return self.dataSouece.count;
}

-(BaseModel *)modelForCell:(NSIndexPath *)indexPath{
    return self.dataSouece[indexPath.row];
}


- (NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQAccidentTypesTableViewCell"]];
}

//༥༤༽ཀབཀྲ་ཤིས་བདེ་ལེགས།
#pragma mark -- 选中返回
- (IBAction)clickSelect:(UIButton *)sender {
    NSMutableArray * arrayM = NSMutableArray.new;
    NSMutableArray * arrayMTitle = NSMutableArray.new;
    NSMutableArray * arrayMID = NSMutableArray.new;
    for (SQAccidentTypesModelData * modelData in self.dataArrayM) {
//        NSDictionary * dic = @{@"sal_operation_link_id":modelData.d_id,@"sal_operation_link_text":modelData.d_name};

        [arrayM addObject:modelData.ep_id];
        [arrayMID addObject:modelData];
        [arrayMTitle addObject:modelData.ep_name];
    }
//
//    NSString * josn = [CrazyFunction crazy_ObjectToJsonString:arrayM];
    NSString * josn = [arrayM componentsJoinedByString:@","];
//
    NSString *string = [arrayMTitle componentsJoinedByString:@","];
//
    self.accidentTypesBack(string, josn, arrayMID);
    [self.navigationController popViewControllerAnimated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
